home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / MiscKit1.7.1 / MiscKit / Examples / FileBrowser / MyController.m < prev    next >
Encoding:
Text File  |  1995-04-12  |  5.6 KB  |  247 lines

  1. //        Written by Todd Thomas Copyright (c) 1995 by Todd Thomas.
  2. //                Version 1.0.  All rights reserved.
  3. //
  4. //        This notice may not be removed from this source code.
  5. //
  6. //    This object is included in the MiscKit by permission from the author
  7. //    and its use is governed by the MiscKit license, found in the file
  8. //    "LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
  9. //    for a list of all applicable permissions and restrictions.
  10. //    
  11.  
  12. #import <appkit/appkit.h>
  13. #import <misckit/MiscFile.h>
  14. #import <misckit/MiscAppFile.h>
  15. #import "MyNXBrowserCell.h"
  16. #import "Inspector.subproj/FileInspector.h"
  17. #import "MyController.h"
  18.  
  19.  
  20. @implementation MyController
  21.  
  22. - init
  23. {
  24.     [super init];
  25.     rootFile = [ [MiscFile alloc] initWithPath: "/"];
  26.     
  27.     // Used so we can create children of different classes depending upon
  28.     // their extension. For this example, there is just one subclass of
  29.     // MiscFile which will be used for any file that has the "app" extension.
  30.     [rootFile setCreateChildrenUsingExtensions: YES];
  31.     [MiscFile registerFileClass: [MiscAppFile class] forExtension: "app"];
  32.     inspector = nil;
  33.     
  34.     return self;
  35. }
  36.  
  37.  
  38. - free
  39. {
  40.     [rootFile free];
  41.     return [super free];
  42. }
  43.  
  44.  
  45. - updateDirectory: sender
  46. {
  47.     // Called when we click the "Update Directory" button.
  48.       id  selectedFile = [ [browser selectedCell] file];
  49.       int  selectedCol = [browser selectedColumn];
  50.   
  51.       if ([selectedFile isDirectory])
  52.     {
  53.         [selectedFile updateChildren];
  54.         selectedCol++;    
  55.      }
  56.     else
  57.         [ [ [selectedFile parents] objectAt: 0] updateChildren];
  58.  
  59.     [browser reloadColumn: selectedCol];    
  60.     
  61.     return self;
  62. }
  63.  
  64.  
  65. - showInspector: sender
  66. {
  67.     // Load the inspector if it hasn't been already.
  68.     if (inspector == nil)
  69.     {
  70.         char  path[MAXPATHLEN+1];
  71.         
  72.         if ([ [NXBundle mainBundle] getPath: path forResource: "Inspector"
  73.                     ofType: "nib"])
  74.             [NXApp loadNibFile: path owner: self];
  75.         
  76.      }
  77.  
  78.     // Display the inspector window.
  79.     [self inspectFile: nil];
  80.     [ [inspector window] orderFront: nil];
  81.     return self;
  82. }
  83.  
  84.  
  85. - inspectFile: sender
  86. {
  87.     // If the inspector is loaded then let it know there is a new
  88.     // file to look at.
  89.     if (inspector != nil)
  90.     {
  91.         if ([ [sender selectedCell] file] == nil)
  92.             [inspector selectedFile: rootFile];
  93.         else    
  94.             [inspector selectedFile: [ [sender selectedCell ] file] ];
  95.      }
  96.     return self;
  97. }
  98.  
  99.  
  100. - duplicateFile: sender
  101. {    
  102.     // When the menu item "Duplicate file" is clicked upon, get the selected
  103.     // item and try to duplicate it.
  104.     MiscFile  *selFile = [ [browser selectedCell] file];
  105.     char  tmp[MAXPATHLEN+1];
  106.     char  *ptr;
  107.     
  108.     if (selFile == nil)
  109.         return self;
  110.     
  111.     strcpy (tmp, [selFile fullPath]);
  112.     ptr = strrchr (tmp, '/');
  113.     *(ptr+1) = '\0';
  114.     
  115.     strcat (tmp, "CopyOf");
  116.     strcat (tmp, [selFile filename]);
  117.     
  118.     if ([selFile createCopyNamed: tmp] == nil)
  119.         NXRunAlertPanel ("Oh no!", "Couldn't create the copy. Sorry.", 
  120.                         "OK", 0, 0);
  121.     
  122.     [self updateDirectory: nil];
  123.     return self;
  124. }
  125.     
  126.     
  127. - createSymLink: sender
  128. {
  129.     // Create a symbolic link to the selected file in the browser.
  130.     MiscFile  *selFile = [ [browser selectedCell] file];
  131.     char  tmp[MAXPATHLEN+1];
  132.     char  *ptr;
  133.     
  134.     if (selFile == nil)
  135.         return self;
  136.     
  137.     strcpy (tmp, [selFile fullPath]);
  138.     ptr = strrchr (tmp, '/');
  139.     *(ptr+1) = '\0';
  140.     
  141.     strcat (tmp, "SymLinkOf");
  142.     strcat (tmp, [selFile filename]);
  143.     
  144.     if ([selFile createSymbolicLinkNamed: tmp] == nil)
  145.         NXRunAlertPanel ("Oh no!", "Couldn't create the symbolic link. Sorry.", 
  146.                         "OK", 0, 0);
  147.     
  148.     [self updateDirectory: nil];
  149.     return self;
  150. }
  151.  
  152.  
  153. - createHardLink: sender
  154. {
  155.     // Create a hard link to the selected file in the browser.
  156.     MiscFile  *selFile = [ [browser selectedCell] file];
  157.     char  tmp[MAXPATHLEN+1];
  158.     char  *ptr;
  159.     
  160.     if (selFile == nil)
  161.         return self;
  162.     
  163.     strcpy (tmp, [selFile fullPath]);
  164.     ptr = strrchr (tmp, '/');
  165.     *(ptr+1) = '\0';
  166.     
  167.     strcat (tmp, "HardLinkOf");
  168.     strcat (tmp, [selFile filename]);
  169.     
  170.     if ([selFile createHardLinkNamed: tmp] == nil)
  171.         NXRunAlertPanel ("Oh no!", "Couldn't create the link. Sorry.", 
  172.                         "OK", 0, 0);
  173.     
  174.     [self updateDirectory: nil];
  175.     return self;
  176. }
  177.  
  178. @end
  179.  
  180.  
  181. @implementation MyController (NibInitialization)
  182.  
  183. // Do some initialization (especially to the browser).
  184.  
  185. - awakeFromNib
  186. {
  187.     // Use new browser cell to keep track of underlying MiscFile each cell 
  188.     // represents.
  189.     [browser setCellClass: [MyNXBrowserCell class] ];
  190.     
  191.     // The delegate is set here instead of in IB because when it is set 
  192.     // in IB, loadColumnZero gets called (from within drawSelf::) before 
  193.     // this awakeFromNib does, and since the new browser cell isn't set 
  194.     // yet, it crashes. I mailed it to Bug_NeXT.
  195.     
  196.     [browser setDelegate: self];
  197.     [browser setTarget: self];
  198.     [browser setAction: @selector(inspectFile:)];
  199.     [browser loadColumnZero];
  200.     return self;
  201. }
  202.  
  203. @end
  204.  
  205.  
  206. @implementation MyController (BrowserDelegate)
  207.  
  208. // Load up whatever column we are supposed to. If we are loading the
  209. // first one just use the rootFile, else we have to ask the 
  210. // rightmost selected cell to tell us the MiscFile it represents.
  211.  
  212. - (int)browser:sender fillMatrix:matrix inColumn:(int)column
  213. {
  214.   id  displayFile = nil;
  215.   id  children = nil;
  216.   id  child = nil;
  217.   id  cell = nil;
  218.   int  i;
  219.  
  220.     // determine which MiscFile to use   
  221.     if (column == 0)    
  222.         displayFile = rootFile;
  223.     else
  224.         // get the MiscFile represented by the rightmost selected cell
  225.         displayFile = [ [browser selectedCell] file];
  226.         
  227.     children = [displayFile children];
  228.  
  229.     // Load up the matrix with the MiscFile's children.
  230.         
  231.     for (i=0; i<[children count]; i++)
  232.     {
  233.         [matrix insertRowAt: i];
  234.         cell = [matrix cellAt: i :0];
  235.         child = [children objectAt: i];
  236.         [cell setStringValue: [child filename] ];
  237.         [cell setFile: child];    // This is so each cell knows it's MiscFile
  238.         [cell setLoaded: YES];
  239.         [cell setLeaf: [child displayAsLeaf] ];
  240.      }
  241.  
  242.     return [children count];
  243. }
  244.  
  245. @end
  246.  
  247.